<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Getter | Vuex</title>
    <meta name="description" content="Vue.js 的中心化状态管理方案">
    <link rel="icon" href="logo.png">
  <link rel="apple-touch-icon" href="https://vuex.vuejs.org/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="https://vuex.vuejs.org/icons/safari-pinned-tab.svg" color="#3eaf7c">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    
    <link rel="preload" href="https://vuex.vuejs.org/assets/css/0.styles.04c31208.css" as="style"><link rel="preload" href="https://vuex.vuejs.org/assets/js/app.92019be7.js" as="script"><link rel="preload" href="https://vuex.vuejs.org/assets/js/102.9eb5b6b9.js" as="script"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/10.9ff0229a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/100.ff89a77e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/101.75e31694.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/103.84f39ee4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/104.1d8f5eda.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/105.22d02a0b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/106.6c7f9f4f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/107.df69838a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/108.57710a8f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/109.0b9350df.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/11.22ec5291.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/110.da276e13.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/111.d271062b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/112.fff28bd3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/12.4bb08316.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/13.eb7734ad.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/14.189de3ec.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/15.96301577.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/16.d35e6c41.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/17.6932f877.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/18.14d47735.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/19.f57a3619.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/20.4b3b43ce.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/21.fdbf3e9a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/22.38a06d07.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/23.20e52ca0.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/24.38c1ec6f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/25.d0749285.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/26.fa0199ca.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/27.104f01d5.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/28.0502b1eb.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/29.a3deb738.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/3.c2431962.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/30.5f61f97c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/31.c13e234c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/32.3346e47e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/33.9552f2b4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/34.22c87151.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/35.e274dae2.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/36.1f72edfe.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/37.3c8f5a6b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/38.18505477.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/39.fa1ab3f0.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/4.6114c173.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/40.82b0bf0f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/41.83249a92.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/42.dedbcb2b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/43.1626a294.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/44.46f3ccfa.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/45.5e63c438.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/46.4837dfdd.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/47.507f6518.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/48.56ef6c75.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/49.14767935.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/5.fea3e8c1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/50.a2cf5518.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/51.9a6f14ab.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/52.21aa240c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/53.a7ca4e09.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/54.18643ae3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/55.48be8fde.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/56.8123f4e5.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/57.3f666e3d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/58.a7e61993.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/59.8be4dcd4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/6.e165131f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/60.b6d2d182.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/61.eca98d13.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/62.5ee1323a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/63.c36ff790.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/64.b063e22d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/65.d5a56cb7.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/66.32060737.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/67.7b1f4ad9.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/68.61c1fb5a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/69.9eecf973.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/7.6a20bd14.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/70.5012a640.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/71.76c479ea.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/72.25f5be51.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/73.d31aea65.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/74.98a6bff3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/75.fa397fe2.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/76.8b008b0b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/77.280e8204.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/78.ba855657.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/79.58af331f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/8.259b04b8.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/80.a0635feb.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/81.762ec5d1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/82.0d4604b1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/83.9d064f79.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/84.72918f87.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/85.0d9821f3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/86.1631e292.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/87.6c6ced24.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/88.c8dfc6b4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/89.3e00d819.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/9.e8b16084.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/90.cd40ed9e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/91.71809249.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/92.aba0a727.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/93.c9b387fd.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/94.5be4c560.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/95.a52fb380.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/96.74307a3e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/97.0738b41c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/98.44547c3f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/99.9a86824d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/vendors~docsearch.0cca2532.js">
    
  
<link rel="stylesheet" type="text/css" href="getters.css" media="all">
</head>
<body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a indepth="true" href="index.html" class="home-link router-link-active"><!----> <span class="site-name">Vuex</span></a> <div class="links" style="max-width:nullpx;"><form action="https://vuex.vuejs.org/zh/guide/getters.html" id="search-form" class="algolia-search-wrapper search-box"><input value="" id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"><div class="nav-item"><a indepth="true" href="default.html" class="nav-link router-link-active">指南</a></div><div class="nav-item"><a indepth="true" href="default_001.html" class="nav-link">API 参考</a></div><div class="nav-item"><a href="https://github.com/vuejs/vuex/releases" target="_top" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display: none;"><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/guide/getters.html" class="nav-link">English</a></li><li class="dropdown-item"><!----> <a indepth="true" href="getters.html" class="nav-link router-link-exact-active router-link-active">简体中文</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ja/guide/getters.html" class="nav-link">日本語</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ru/guide/getters.html" class="nav-link">Русский</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/kr/guide/getters.html" class="nav-link">한국어</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ptbr/guide/getters.html" class="nav-link">Português</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/fr/guide/getters.html" class="nav-link">Français</a></li></ul></div></div> <a href="https://github.com/vuejs/vuex" target="_top" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a indepth="true" href="default.html" class="nav-link router-link-active">指南</a></div><div class="nav-item"><a indepth="true" href="default_001.html" class="nav-link">API 参考</a></div><div class="nav-item"><a href="https://github.com/vuejs/vuex/releases" target="_top" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display: none;"><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/guide/getters.html" class="nav-link">English</a></li><li class="dropdown-item"><!----> <a indepth="true" href="getters.html" class="nav-link router-link-exact-active router-link-active">简体中文</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ja/guide/getters.html" class="nav-link">日本語</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ru/guide/getters.html" class="nav-link">Русский</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/kr/guide/getters.html" class="nav-link">한국어</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ptbr/guide/getters.html" class="nav-link">Português</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/fr/guide/getters.html" class="nav-link">Français</a></li></ul></div></div> <a href="https://github.com/vuejs/vuex" target="_top" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><a indepth="true" href="installation.html" class="sidebar-link">安装</a></li><li><a indepth="true" href="index.html" class="sidebar-link">Vuex 是什么？</a></li><li><a indepth="true" href="default.html" class="sidebar-link">开始</a></li><li><div class="sidebar-group"><p class="sidebar-heading open">核心概念 <!----></p> <ul class="sidebar-group-items"><li><a indepth="true" href="state.html" class="sidebar-link">State</a></li><li><a indepth="true" href="getters.html" class="active sidebar-link">Getter</a><ul class="sidebar-sub-headers"></ul></li><li><a indepth="true" href="mutations.html" class="sidebar-link">Mutation</a></li><li><a indepth="true" href="actions.html" class="sidebar-link">Action</a></li><li><a indepth="true" href="modules.html" class="sidebar-link">Module</a></li></ul></div></li><li><a indepth="true" href="structure.html" class="sidebar-link">项目结构</a></li><li><a indepth="true" href="plugins.html" class="sidebar-link">插件</a></li><li><a indepth="true" href="strict.html" class="sidebar-link">严格模式</a></li><li><a indepth="true" href="forms.html" class="sidebar-link">表单处理</a></li><li><a indepth="true" href="testing.html" class="sidebar-link">测试</a></li><li><a indepth="true" href="hot-reload.html" class="sidebar-link">热重载</a></li></ul> </div> <div class="page"> <div class="content"><h1 id="getter"><a href="#getter" aria-hidden="true" class="header-anchor">#</a> Getter</h1> <div class="scrimba"><a href="https://scrimba.com/p/pnyzgAP/c2Be7TB" target="_top" rel="noopener noreferrer">在 scrimba 上尝试这节课</a></div> <p>有时候我们需要从 store 中的 state 中派生出一些状态，例如对列表进行过滤并计数：</p> <div class="language-js extra-class"><pre class="language-js"><code>computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token function">doneTodosCount</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>todo <span class="token operator">=&gt;</span> todo<span class="token punctuation">.</span>done<span class="token punctuation">)</span><span class="token punctuation">.</span>length
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>如果有多个组件需要用到此属性，我们要么复制这个函数，或者抽取到一个共享函数然后在多处导入它——无论哪种方式都不是很理想。</p> <p>Vuex 允许我们在 store 中定义“getter”（可以认为是 store 的计算属性）。就像计算属性一样，getter 的返回值会根据它的依赖被缓存起来，且只有当它的依赖值发生了改变才会被重新计算。</p> <p>Getter 接受 state 作为其第一个参数：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  state<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    todos<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">'...'</span><span class="token punctuation">,</span> done<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">'...'</span><span class="token punctuation">,</span> done<span class="token punctuation">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    doneTodos<span class="token punctuation">:</span> state <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>todo <span class="token operator">=&gt;</span> todo<span class="token punctuation">.</span>done<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="通过属性访问"><a href="#通过属性访问" aria-hidden="true" class="header-anchor">#</a> 通过属性访问</h3> <p>Getter 会暴露为 <code>store.getters</code> 对象，你可以以属性的形式访问这些值：</p> <div class="language-js extra-class"><pre class="language-js"><code>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>doneTodos <span class="token comment">// -&gt; [{ id: 1, text: '...', done: true }]</span>
</code></pre></div><p>Getter 也可以接受其他 getter 作为第二个参数：</p> <div class="language-js extra-class"><pre class="language-js"><code>getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  doneTodosCount<span class="token punctuation">:</span> <span class="token punctuation">(</span>state<span class="token punctuation">,</span> getters<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> getters<span class="token punctuation">.</span>doneTodos<span class="token punctuation">.</span>length
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>doneTodosCount <span class="token comment">// -&gt; 1</span>
</code></pre></div><p>我们可以很容易地在任何组件中使用它：</p> <div class="language-js extra-class"><pre class="language-js"><code>computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token function">doneTodosCount</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>doneTodosCount
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>注意，getter 在通过属性访问时是作为 Vue 的响应式系统的一部分缓存其中的。</p> <h3 id="通过方法访问"><a href="#通过方法访问" aria-hidden="true" class="header-anchor">#</a> 通过方法访问</h3> <p>你也可以通过让 getter 返回一个函数，来实现给 getter 传参。在你对 store 里的数组进行查询时非常有用。</p> <div class="language-js extra-class"><pre class="language-js"><code>getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  getTodoById<span class="token punctuation">:</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>todo <span class="token operator">=&gt;</span> todo<span class="token punctuation">.</span>id <span class="token operator">===</span> id<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span><span class="token function">getTodoById</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// -&gt; { id: 2, text: '...', done: false }</span>
</code></pre></div><p>注意，getter 在通过方法访问时，每次都会去进行调用，而不会缓存结果。</p> <h3 id="mapgetters-辅助函数"><a href="#mapgetters-辅助函数" aria-hidden="true" class="header-anchor">#</a> <code>mapGetters</code> 辅助函数</h3> <p><code>mapGetters</code> 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> mapGetters <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vuex'</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  <span class="token comment">// 使用对象展开运算符将 getter 混入 computed 对象中</span>
    <span class="token operator">...</span><span class="token function">mapGetters</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
      <span class="token string">'doneTodosCount'</span><span class="token punctuation">,</span>
      <span class="token string">'anotherGetter'</span><span class="token punctuation">,</span>
      <span class="token comment">// ...</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>如果你想将一个 getter 属性另取一个名字，使用对象形式：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">mapGetters</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`</span>
  doneCount<span class="token punctuation">:</span> <span class="token string">'doneTodosCount'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div></div> <div class="page-edit"><!----> <!----></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a indepth="true" href="state.html" class="prev">
          State
        </a></span> <span class="next"><a indepth="true" href="mutations.html">
          Mutation
        </a>
        →
      </span></p></div> <div class="bsa-cpc-wrapper"><div class="bsa-cpc"></div></div></div> <!----></div></div>
    
  




</body></html>
